using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using NPOI.POIFS.FileSystem;

namespace JNPF.Basics.Logic
{
    public class CustomeruserBll
    {
        private CustomeruserIService service = new CustomeruserService();


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomeruserEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<CustomeruserEntity> GetInfo(int customerId, int type)
        {
            try
            {
                return await service.GetInfo(customerId, type);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomeruserEntity>> GetPageList(CustomeruserListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomeruserEntity entity)
        {
            try
            {
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.CreatorUser = UserProvider.Instance.Get().UserName;
                entity.DeleteMark = 0;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, CustomeruserEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task allot(int customerId, List<CustomeruserCrInput> customeruserCrInputs)
        {
            try
            {
                CustomerbaseEntity customerbase = await new CustomerbaseBll().GetInfo(customerId);
                customerbase.CurrentState = 1;




                CustomeruserEntity customeruserEntity = null;
                List<CustomeruserEntity> customeruserEntities = new List<CustomeruserEntity>();

                int number = 1;
                foreach (var item in customeruserCrInputs)
                {
                    customeruserEntity = new CustomeruserEntity();
                    customeruserEntity.Type = item.type;
                    customeruserEntity.UserId = item.userId;
                    customeruserEntity.CustomerId = item.customerId;
                    customeruserEntity.SortCode = number++;
                    customeruserEntity.DeleteMark = 0;
                    customeruserEntity.EnabledMark = 1;
                    customeruserEntity.CreatorTime = DateTime.Now;
                    customeruserEntity.CreatorUserId = UserProvider.Instance.Get().UserId;
                    customeruserEntity.CreatorUser = UserProvider.Instance.Get().UserName;
                    customeruserEntities.Add(customeruserEntity);
                }

                var old = await service.GetCustomeruserByCustomerId(customerId);

                await service.allot(customerbase, old, customeruserEntities);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }




        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomeruserEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<CustomeruserEntity>> GetList(CustomeruserListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}